package cn.ztydata.teachingsystem.heaven.dao;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 专业群组Dao类
 *
 * @author lyj
 * @since 2015/5/6
 */
@Repository
public class SpecialtyGroupDao extends BaseDao{

    /**
     * 根据专业ID获取负责人ID
     * @param id
     * @return String
     * @author lyj
     * @since 2015-5-7
     */
    public Long getLeaderById(final long id) {
        StringBuilder sql = new StringBuilder("SELECT iLeaderId FROM t_specialty WHERE id = :id");

        try {
            return namedParameterJdbcTemplate.queryForObject(sql.toString(), new MapSqlParameterSource().addValue("id", id), new RowMapper<Long>() {
                @Override
                public Long mapRow(ResultSet resultSet, int i) throws SQLException {
                    return resultSet.getLong("iLeaderId");
                }
            });
        } catch (EmptyResultDataAccessException ex) {
            log.warn("获取专业负责人ID失败！");
            return Long.valueOf(0);
        }
    }
}
